Security News
Python Overtakes JavaScript as Top Programming Language on GitHub
Python becomes GitHub's top language in 2024, driven by AI and data science projects, while AI-powered security tools are gaining adoption.
import-from
Advanced tools
The npm package 'import-from' allows you to import modules from a specific path, bypassing the default behavior of Node.js module resolution. This can be particularly useful in scenarios where you need to dynamically choose different versions of a module or when working with plugins that may not be in the node_modules directory of the current project.
Import module from a specific directory
This feature allows you to import a module by specifying the directory from which the module should be loaded. This is useful for loading modules dynamically based on runtime conditions or configurations.
const importFrom = require('import-from');
const specificModule = importFrom('/path/to/specific/directory', 'module-name');
Silent import attempt
This feature provides a way to attempt to import a module silently without throwing errors if the module does not exist. It is useful for optional dependencies that may or may not be present in a given environment.
const importFrom = require('import-from');
const tryImport = importFrom.silent('/path/to/directory', 'optional-module');
This package allows you to require modules from a string of code. It is similar to 'import-from' in that it modifies the standard module loading behavior, but it focuses on executing and importing code from strings rather than from specific file paths.
Proxyquire is designed to help with mocking dependencies for testing purposes. It allows you to replace modules in require calls within the module under test. While it also changes how modules are loaded, it is specifically tailored for testing rather than general module loading from different locations.
Import a module like with
require()
but from a given path
$ npm install import-from
const importFrom = require('import-from');
// There is a file at `./foo/bar.js`
importFrom('foo', './bar');
Like require()
, throws when the module can't be found.
Returns undefined
instead of throwing when the module can't be found.
Type: string
Directory to import from.
Type: string
What you would use in require()
.
Create a partial using a bound function if you want to import from the same fromDir
multiple times:
const importFromFoo = importFrom.bind(null, 'foo');
importFromFoo('./bar');
importFromFoo('./baz');
FAQs
Import a module like with `require()` but from a given path
The npm package import-from receives a total of 5,126,216 weekly downloads. As such, import-from popularity was classified as popular.
We found that import-from demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Python becomes GitHub's top language in 2024, driven by AI and data science projects, while AI-powered security tools are gaining adoption.
Security News
Dutch National Police and FBI dismantle Redline and Meta infostealer malware-as-a-service operations in Operation Magnus, seizing servers and source code.
Research
Security News
Socket is tracking a new trend where malicious actors are now exploiting the popularity of LLM research to spread malware through seemingly useful open source packages.